﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace DMU.Gui.GuiPro
{
    public partial class frmThemNCC : DevExpress.XtraEditors.XtraForm
    {
        private uc_NhaCungCap ucNCC;
        private Service sv=new Service();
        private help hp=new help();

        public frmThemNCC(uc_NhaCungCap uc)
        {
            InitializeComponent();
            ucNCC = uc;
            
        }

        private void chkActive_CheckedChanged(object sender, EventArgs e)
        {

        }

        #region Thao tac theem du lieu hang loat

        
        private SqlParameter[] getPara()
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter("@MaNCC", txtMa.Text);
            pr[1] = new SqlParameter("@TenNCC", txtTen.Text);
            pr[2] = new SqlParameter("@DiaChi", txtdc.Text);
            pr[3] = new SqlParameter("@SDT", txtSDT.Text);
            pr[4] = new SqlParameter("@Fax", txtFax.Text);
            return pr;
        }

        private string[] getInfo()
        {
            string[] st = new string[5];
            st[0] = txtMa.Text;
            st[1] = txtTen.Text;
            st[2] = txtdc.Text;
            st[3] = txtSDT.Text;
            st[4] = txtFax.Text;

            return st;
        }

        private void btnHoanTat_Click(object sender, EventArgs e)
        {
            //if (!dxValidationProvider2.Validate())
            //{
            //    if (cbbPath.Text.Trim() == "")
            //    {
            //        cbbPath.Focus(); return;
            //    }
            //    else
            //    {
            //        cbbSheetName.Focus(); return;
            //    }
            //}
            //DataTable dt;// = ucMH.dtMH.Clone();
            //dt = sv.excelToDatatable(cbbPath.Text, "select MaMH,TenMH,SoTiet,isTC,MaNganh,GhiChu from [" + cbbSheetName.Text + "]");
            
            //DataColumn cl = new DataColumn("IsActive", Type.GetType("System.Boolean"));
            //dt.Columns.Add(cl);
            
            //if (dt.Rows.Count > 0)
            //{
            //    try
            //    {
            //        sv.openConnect();
            //        SqlParameter[] pr; int dem = 0; int rowCount = dt.Rows.Count;
                    
            //        foreach (DataRow row in dt.Rows)
            //        {
            //            if ((row["MaMH"] != null && row["MaMH"].ToString() != "") && (row["MaNganh"]!=null && row["MaNganh"].ToString().Trim()!="" ))
            //            {

            //                if (row[3] == null || row[3].ToString() != "")
            //                    row["isTC"] = true;
            //                else
            //                    row["isTC"] = false;


            //                pr = getPara(row[0].ToString(), row[1].ToString(), row[2].ToString(), bool.Parse(row[3].ToString()), row[4].ToString(), true, row[5].ToString());

            //                if (sv.executeNonqueryStore("dmu_proMThemMH1", pr))
            //                {
            //                    dem++;
            //                    row["IsActive"] =true;
            //                    dt.AcceptChanges();
            //                    try
            //                    {
            //                        ucMH.dtMH.ImportRow(row);
            //                    }
            //                    catch (Exception) { continue; }

            //                }
            //            }
            //        }

            //        dt.Dispose();
            //        hp.showMes("Thêm thành công " + dem + "/" + rowCount, -1);

            //    }
            //    catch (Exception et)
            //    {
            //        hp.showMes("Có lỗi khi thêm !. " + et.Message, 1);
            //    }
            //}

        }

        #endregion

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider1.Validate())
            {
                return;
            }
            try
            {
                if (ucNCC.them)
                {
                    if (sv.executeNonqueryStore("NhaCungCap_Insert",getPara()))
                    {
                        sv.addRow(ucNCC.dtNCC,getInfo());
                        txtMa.Focus();
                        txtMa.Text = txtTen.Text = "";
                    }

                }
                else
                {

                    if (sv.executeNonqueryStore("NhaCungCap_Update",getPara()))
                    {
                        sv.updateRow(ucNCC.dtNCC.Rows[ucNCC.BindingContext[ucNCC.dtNCC].Position],getInfo());
                        this.Close();
                    }
                    else
                        hp.showMes("Có lỗi khi sữu !. Vui lòng thử lại ", -1);
                }
            }
            catch (Exception et)
            {
                hp.showMes("Thao tác không thành công !. Vui lòng thử lại " + et.Message, 1);
            }

        }


        private void cbbPath_ButtonClick_1(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string path=hp.openFile();
            if (path != "")
            {
                try
                {
                    cbbPath.Text = path;
                    cbbSheetName.Properties.Items.Clear();
                    cbbSheetName.Properties.Items.AddRange(sv.getEcelSheetName(path));
                    cbbSheetName.Focus();
                }
                catch (Exception et) { hp.showMes("Lỗi khi load sheetName. Vui lòng thử lại ! " + et.Message, 1); }
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmThemNCC_Load(object sender, EventArgs e)
        {

        }
    }
}